﻿@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4" id="myToken">Welcome</h1>
    <button id="btnAction1">Action1</button>
    <button id="btnAction2">Action2</button>
    <button id="btnAction3">Action3</button>
</div>
<script>
    var myToken=localStorage.getItem("mytoken");
    // $.ajax({
    //     url:"/myapi/DoCheckToken",
    //     type:"POST",
    //     headers:{
    //         "Authorization":"Bearer "+myToken
    //     },
    //     success:function(d){
    //         if(d.code==1){
    //             $("#myToken").html(myToken);
    //         }
    //         else{
    //             alert("登录已过期，请重新登录");
    //             location.href="/home/login";
    //         }
    //     }
    // });

    $.ajax({
        url:"/myapi/DoGetUser?name=admin",
        type:"POST",
        headers:{
            "Authorization":"Bearer "+myToken
        },
        success:function(d){
            if(d.code==1){
                $("#myToken").html(JSON.stringify( d.user));
            }
            else{
                alert("登录已过期，请重新登录");
                location.href="/home/login";
            }
        },
        error:function(xhr,status,error){
            if(xhr.status==401){
                 alert("未登录，请重新登录");
                location.href="/home/login";
            }
            if(xhr.status==403){
                 alert("没有权限，请重新登录");
                location.href="/home/login";
            }
        }
    });

    $("#btnAction1").click(function(){
        $.ajax({
        url:"/myapi/DoAction1",
        type:"POST",
        headers:{
            "Authorization":"Bearer "+myToken
        },
        success:function(d){
            if(d.code==1){
                alert("已授权");
            }
        },
        error:function(xhr,status,error){
            if(xhr.status==401){
                 alert("未登录，请重新登录");
                location.href="/home/login";
            }
            if(xhr.status==403){
                 alert("没有权限");
            }
        }
    });
    });
    $("#btnAction2").click(function(){
        $.ajax({
        url:"/myapi/DoAction2",
        type:"POST",
        headers:{
            "Authorization":"Bearer "+myToken
        },
        success:function(d){
            if(d.code==1){
                alert("已授权");
            }
        },
        error:function(xhr,status,error){
            if(xhr.status==401){
                 alert("未登录，请重新登录");
                location.href="/home/login";
            }
            if(xhr.status==403){
                 alert("没有权限");
            }
        }
    });
    });
    $("#btnAction3").click(function(){
        $.ajax({
        url:"/myapi/DoAction3",
        type:"POST",
        headers:{
            "Authorization":"Bearer "+myToken
        },
        success:function(d){
            if(d.code==1){
                alert("已授权");
            }
        },
        error:function(xhr,status,error){
            if(xhr.status==401){
                 alert("未登录，请重新登录");
                location.href="/home/login";
            }
            if(xhr.status==403){
                 alert("没有权限");
            }
        }
    });
    });
</script>

